package org.msfj.common.model;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class KeyValuePair<String, V> {
    private String key;
    private V value;


    public KeyValuePair() {
    }

    public KeyValuePair(String key, V value) {
        this.key = key;
        this.value = value;
    }

    public String getKey() {
        return key;
    }

    public void setKey(String key) {
        this.key = key;
    }

    public void setValue(V value) {
        this.value = value;
    }

    public V getValue() {
        return value;
    }

    /**
     * 转换 list 为 map
     *
     * @param keyValuePairs
     * @param valueClass
     * @param <V> 值的类型
     * @return
     */
    public static <V> Map<java.lang.String, V> toMap(List<KeyValuePair> keyValuePairs, Class<V> valueClass) {

        if (keyValuePairs != null) {
            Map<java.lang.String, V> hashmap = new HashMap<>();
            for (KeyValuePair keyValuePair : keyValuePairs) {
                Object value = keyValuePair.getValue();
                if (valueClass == Long.class) {
                    value = Long.valueOf(value.toString());
                } else if (valueClass == Integer.class) {
                    value = Integer.valueOf(value.toString());
                } else if (valueClass == java.lang.String.class) {
                    value = value.toString();
                }
                hashmap.put(keyValuePair.getKey().toString(), (V) value);
            }
            return hashmap;
        }
        return null;
    }
}
